//
//  AddAddressView.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/16.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class AddAddressView: UIView {

    // MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initBaseLayout()
        LayoutPageSubviews()
        configUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(leftLabel)
        self.addSubview(textView)
        self.addSubview(placeholderLabel)
        self.addSubview(lineView)
        self.addSubview(arrowImageView)
    }
    
    func LayoutPageSubviews() {
        leftLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.top.equalTo(self).offset(13)
            make.width.equalTo(80)
            make.height.equalTo(14)
        }
        
//        textView.snp.makeConstraints { (make) in
//            make.left.equalTo(leftLabel.snp.right).offset(0)
//            make.centerY.equalTo(self)
//            make.right.equalTo(self).offset(0)
//            make.height.equalTo(44)
//        }
        
        placeholderLabel.snp.makeConstraints { (make) in
            make.left.equalTo(leftLabel.snp.right).offset(2)
            make.top.equalTo(self).offset(12)
            make.width.equalTo(143)
            make.height.equalTo(17)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.bottom.equalTo(self).offset(0)
            make.width.equalTo(SCREEN_WIDTH-14)
            make.height.equalTo(1)
        }
        
        arrowImageView.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.centerY.equalTo(self)
            make.width.equalTo(6)
            make.height.equalTo(12)
        }
    }
    func configUI() {
    }
    
    // MARK: - setters and getters
    var _index: Int?
    var index: Int? {
        set {
            if newValue == 2 || newValue == 3 || newValue == 4 {
                arrowImageView.isHidden = false
            } else {
                arrowImageView.isHidden = true
            }
            if newValue == 5 {
                _textView = UITextView(frame: CGRect(x: 94, y: 6, width: SCREEN_WIDTH-94, height: 74))
            }
        } get {
            return _index
        }
    }
    
    var _leftTittle: String?
    var leftTittle: String? {
        set {
            leftLabel.text = newValue
        } get {
            return _leftTittle
        }
    }
    
    var _placeholderStr: String?
    var placeholderStr: String? {
        set {
            placeholderLabel.text = newValue
        } get {
            return _placeholderStr
        }
    }
    
    var _textView: UITextView!
    var textView: UITextView {
        if _textView == nil {
            _textView = UITextView(frame: CGRect(x: 94, y: 6, width: SCREEN_WIDTH-94, height: 38))
            _textView.delegate = self
        }
        return _textView
    }
    
    var _leftLabel: UILabel!
    var leftLabel: UILabel {
        if _leftLabel == nil {
            _leftLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 94, height: 14))
            _leftLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _leftLabel.textColor = UIColor(hexString: "#252525")
        }
        return _leftLabel
    }
    
    var _placeholderLabel: UILabel!
    var placeholderLabel: UILabel {
        if _placeholderLabel == nil {
            _placeholderLabel = UILabel()
            _placeholderLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _placeholderLabel.textColor = UIColor(hexString: "#BBBBBB")
        }
        return _placeholderLabel
    }
    
    var _lineView: UIView!
    var lineView: UIView {
        if _lineView == nil {
            _lineView = UIView()
            _lineView.backgroundColor = UIColor(hexString: "#DDDDDD")
        }
        return _lineView
    }
    
    var _arrowImageView: UIImageView!
    var arrowImageView: UIImageView {
        if _arrowImageView == nil {
            _arrowImageView = UIImageView()
            _arrowImageView.image = UIImage(named: "icon_arrow_r")
        }
        return _arrowImageView
    }


}

extension AddAddressView: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        if textView.text == "" {
            placeholderLabel.isHidden = false
        } else {
            placeholderLabel.isHidden = true
        }
    }
}
